package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.util.*;
import com.softer.wxzj.entity.ExpProjectReq;
import com.softer.wxzj.entity.ExpSurveyInfo;
import com.softer.wxzj.entity.FileVO;
import com.softer.wxzj.mapper.ExpProjectReqMapper;
import com.softer.wxzj.mapper.ExpSurveyInfoMapper;
import com.softer.wxzj.service.ExpSurveyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/7/17
 */
@Service
public class ExpSurveyInfoServiceImpl implements ExpSurveyInfoService {

    @Autowired
    private ExpSurveyInfoMapper expSurveyInfoMapper;
    @Autowired
    private ExpProjectReqMapper expProjectReqMapper;
    @Autowired
    private OperUtil zoneCodeUtil;
    @Autowired
    private FileServiceImpl fileService;

    /**
     * 新增现场勘查
     *
     * @param expSurveyInfo 现场勘查
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult add(ExpSurveyInfo expSurveyInfo) {
        if (StringUtils.isEmpty(expSurveyInfo.getCreater())) {
            String userName = ShiroUserUtil.getUserName();
            expSurveyInfo.setCreater(userName);
            expSurveyInfo.setModifier(userName);
        }
        String uuid = UUIDUtil.getUUID();
        expSurveyInfo.setId(uuid);
        //勘察编号
        expSurveyInfo.setSurveyNumber(NoUtil.getOrderNo());
        int num = expSurveyInfoMapper.insert(expSurveyInfo);
        if (num <= 0) {
            return CommonResult.ResponseResult(num);
        }
        //保存图片
        CommonResult commonResult = fileService.addFile(expSurveyInfo.getFileTypeList(), uuid);
        if (ShiroResultEnum.FAIL.getCode() == commonResult.getData()) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), commonResult.getMessage());
        }
        return CommonResult.idResult(num, uuid);
    }

    /**
     * 修改现场勘查
     *
     * @param expSurveyInfo 现场勘查
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult update(ExpSurveyInfo expSurveyInfo) {
        expSurveyInfo.setModifier(StringUtils.isEmpty(expSurveyInfo.getModifier()) ? ShiroUserUtil.getUserName() : expSurveyInfo.getModifier());
        int num = expSurveyInfoMapper.updateById(expSurveyInfo);
        if (num <= 0) {
            return CommonResult.ResponseResult(num);
        }
        //处理图片修改
        CommonResult result = fileService.updateFile(expSurveyInfo.getFileTypeList(), expSurveyInfo.getId());
        if (ShiroResultEnum.FAIL.getCode() == result.getData()) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), result.getMessage());
        }
        return CommonResult.ResponseResult(num);
    }

    /**
     * 刪除现场勘查
     *
     * @param id 现场勘查ID
     * @return 结果
     */
    @Override
    public CommonResult delete(String id) {
        return CommonResult.ResponseResult(expSurveyInfoMapper.deleteByPrimaryKey(id));
    }

    /**
     * 查詢现场勘查
     *
     * @param id      现场勘查ID
     * @param arcType 图档大类
     * @return 现场勘查详情
     */
    @Override
    public ExpSurveyInfo getInfo(String id, String arcType) {
        ExpSurveyInfo surveyInfo = expSurveyInfoMapper.selectByPrimaryKey(id);
        if (StringUtils.isNotEmpty(surveyInfo.getReqId())) {
            ExpProjectReq exp = expProjectReqMapper.selectByPrimaryKey(surveyInfo.getReqId());
            surveyInfo.setReqName(exp.getReqName());
        }
        // 查询图档信息
        List<FileVO> fileVOList = fileService.getFileVOList(id, arcType);
        surveyInfo.setFileTypeList(fileVOList);
        return surveyInfo;
    }

    /**
     * 查询所有勘查记录
     *
     * @param expSurveyInfo 查询实体
     * @return 列表
     */
    @Override
    public List<ExpSurveyInfo> getAllList(ExpSurveyInfo expSurveyInfo) {
        //分精确查询和模糊查询 按创建时间排序
        // expSurveyInfo包含reqId,surveyOper,startTime,endTime,surveyInfo,surveyNumber
        List<ExpSurveyInfo> list;
        if (StringUtils.isNotEmpty(expSurveyInfo.getFuzzy())) {
            list = expSurveyInfoMapper.getFuzzyAllList(expSurveyInfo);
        } else {
            list = expSurveyInfoMapper.getAllList(expSurveyInfo);
        }
        return list;
    }

    /**
     * 现场勘查列表
     *
     * @param page          分页实体
     * @param expProjectReq 查询实体
     * @return 列表
     */
    @Override
    public PageInfo<ExpProjectReq> getProjectList(Page page, ExpProjectReq expProjectReq) {
        String zoneCode = zoneCodeUtil.getZoneCode();
        expProjectReq.setZoneCode(zoneCode);
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        // 分精确查询和模糊查询
        List<ExpProjectReq> list;
        if (StringUtils.isNotEmpty(expProjectReq.getFuzzy())) {
            list = expSurveyInfoMapper.getProjectListFuzzy(expProjectReq.getFuzzy(), zoneCode);
        } else {
            list = expSurveyInfoMapper.getProjectList(expProjectReq);
        }
        PageInfo<ExpProjectReq> info = new PageInfo<>(list);
        return info;
    }

}
